package com.rn.ddd.infrastructure.base;

import com.rn.ddd.infrastructure.util.CopyUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门店数据转换器
 *
 * @author 然诺
 * @date 2021/8/15
 */
public class BaseDataConverter {

    public static <S, T> T convert(S source, Class<T> type) {
        if (source == null) {
            throw new IllegalArgumentException("source is null");
        }
        return CopyUtils.copy(source, type);
    }

    public static <S, T> List<T> convertAll(Collection<S> source, Class<T> type) {
        return source == null || source.isEmpty() ? Collections.emptyList() :
                source.stream().map(s -> convert(s, type)).collect(Collectors.toList());
    }

}
